﻿using System;
using System.Collections;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using Meotom.Upload.v2.Data.Utilities;
using Meotom.Upload.v2.Services;

namespace Meotom.Upload.v2
{
    public partial class Redirected : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Convert.ToString(Page.RouteData.Values["PhotoFrom"]) == "Account")
            {
                liComputer.Attributes.Remove("class");
                liAccount.Attributes.Add("class", "selected");

                lnkDelete.Visible = true;
                //if (!HttpContext.Current.User.Identity.IsAuthenticated)
                //{
                //    lnkDelete.Visible = false;
                //}

                UploadedPhoto.Controls.Add(LoadControl("~/Controls/FromAccount.ascx"));
                return;
            }

            lnkDelete.Visible = false;
            liAccount.Attributes.Remove("class");
            liComputer.Attributes.Add("class", "selected");

            UploadedPhoto.Controls.Add(LoadControl("~/Controls/FromComputer.ascx"));
        }

        protected void BtnUploadUrl_OnClick(object sender, EventArgs e)
        {
            var strUserName = Common.Instance.GetUploadByUser();
            if (Request["txtUrl"] != null)
            {
                var arrStr = new ArrayList();
                foreach (var strUrl in
                    Request["txtUrl"].Split(Convert.ToChar(",")).Where(strUrl => Users.PreAdd(arrStr, strUrl)))
                {
                    arrStr.Add(strUrl);
                }
                
                for (var index = 0; index <= arrStr.Count - 1; index++)
                {
                    Users.PreDownload(arrStr[index].ToString(), strUserName, HttpContext.Current);
                }
            }
            Response.Redirect(HttpContext.Current.User.Identity.IsAuthenticated ? "/Home/Photos/Account" : "/Home/Photos/Computer", false);
        }

        protected void LnkBtnComputer_OnClick(object sender, EventArgs e)
        {
            Response.Redirect("/Home/Photos/Computer");
        }

        protected void LnkBtnAccount_OnClick(object sender, EventArgs e)
        {
            Response.Redirect("/Home/Photos/Account");
        }

        //protected void DropSelectPage_OnSelectedIndexChanged(object sender, EventArgs e)
        //{
        //    if (DropSelectPage.SelectedValue == "0") return;
        //    //PageSize = int.Parse(DropSelectPage.SelectedValue);
        //    //dtPager.PageSize = PageSize;

        //    BindData();
        //}


        protected void lnkDelete_OnClick(object sender, EventArgs e)
        {
            foreach (var str in Regex.Split(Request.Form["cboCheck"], ","))
            {
                if (Common.Instance.IsNumeric(str))
                {
                    Data.BLL.Uploads.Instance.DeleteTemp(Convert.ToInt32(str), Common.Instance.GetUploadByUser());
                }
            }
            Response.Redirect(HttpContext.Current.User.Identity.IsAuthenticated ? "/Home/Photos/Account" : "/Home/Photos/Computer", false);
        }
    }
}